var app = getApp();
Page({
    data:{
        curPage : '/zofui_sales/pages/ucard/ucard',
        page : {
            isend : false,
            doing : false,
            doo : 'ucard',
            pdata : {
                op : 'ucardprize',
                page : 1,
                ulevel : 0,
                sortid : 0,
                ustyle : 1,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        isIPX : app.globalData.isIPX,
        membersnum : 0,
        prompt : [{placeholder:'输入激活码',response:'code'}],
        buttons : [{
            type: "default",
            text: "取消",
            fn: "cancel"
        }, {
            type: "primary",
            text: "确定",
            fn: "confirm"
        }],
        prizelist : false,
        maincolor : '',
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        Time : app.globalData.Time,
        pakpage : {
            paktype : 0,
            pakby : 0,
            pakstyle : 0,
        },
        paktype : [],
        pakby : [],
        pakstyle : [],
        lat : 0,
        lng : 0,
        ulistlevel : [],
        ulestsort : [],
        uliststyle : [],
        pagelist : [],
    },
    onShow: function () {
        wx.hideTabBar();
        if(this.data.inited) this.onLoad(this.data.options);
    },
    onLoad:function(options){
        var self = this;
        
        self.setData({
            options : options,
            zfid : options.zfid ? options.zfid : ( options.scene ? options.scene : 0 ),
        });
        
        app.com.auth(function(user){
            self.getData(false);
            app.com.setBar(app,self);
        });
        app.com.getLocation(self,function(loc){
            self.setData({
                lat : loc.latitude,
                lng : loc.longitude,
            });
        })
    },
    getData(hidestate,fn){
        var self = this;
        var postdata = {
            op:'info',
            zfid:self.data.zfid,
            hidestate:hidestate,
            type : 1,
            pagename : 'ucard',
        };

        app.com.http('ucard','POST',postdata,0,false,'','',function(res){
            if (res.data && !res.data.errno) {
                let indata = res.data.data;
                self.setData({
                    inited : true,
                    cardinfo : res.data.data.cardinfo,
                    userinfo : res.data.data.userinfo,
                })

                var op = self.data.membersnum*1 > 0 ? self.data.page.pdata.op : (indata.params.pencardt == 1 && indata.params.ucardtype == 0 && indata.params.pagetype == 0 ? 'ucardprize' : 'ulist');
                var membersnum = self.data.membersnum*1 > 0 ? self.data.membersnum : (indata.params.pencardt == 1 && indata.params.ucardtype == 0 ? 0 : 1);

                setTimeout(function(){
                    self.setData({
                        Pagetheme : res.data.data.params.style,
                        params : res.data.data.params,
                        sharedata : res.data.data.sharedata,
                        allcard : res.data.data.allcard,
                        'page.pdata.op' : op,
                        membersnum : membersnum,
                        ucardcoup : indata.ucardcoup ? indata.ucardcoup : [],
                        prizelist : indata.params.ucardtype == 1  ? indata.prizelist : false,
                        paktype : res.data.data.paktype ? res.data.data.paktype : [],
                        pakby : res.data.data.pakby ? res.data.data.pakby : [],
                        pakstyle : res.data.data.pakstyle ? res.data.data.pakstyle : [],
                        ulistlevel : res.data.data.ulistlevel ? res.data.data.ulistlevel : [],
                        ulestsort : res.data.data.ulestsort ? res.data.data.ulestsort : [],
                        uliststyle : res.data.data.uliststyle ? res.data.data.uliststyle : [],
                        pagelist : res.data.data.page ? res.data.data.page : [],
                    },function(){

                        if(fn) fn();
                        if(!hidestate) self.onReachBottom();
                    });

                    var maincolor = app.com.mainColor(indata.params.style);
                    self.setData({
                        maincolor : maincolor,
                    })

                    if( indata.params.ucardtype == 1 ){
                        wx.setNavigationBarColor({
                            frontColor: '#ffffff',
                            backgroundColor: maincolor,
                        });
                        wx.setBackgroundColor({
                            backgroundColor : maincolor
                        })
                    }

                    wx.setNavigationBarTitle({
                        title:  res.data.data.params.title ? res.data.data.params.title : '会员',
                    });
                },5)
            }
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onShareTimeline(){
        return {
            title: this.data.sharedata.title,
            query: this.data.sharedata.query,
            imageUrl : this.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
            datalist : [],
            'page.isbot' : false,
            'page.isend' : false,
            'page.pdata.page' : 1,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        wx.stopPullDownRefresh();
    },
    code(e){
        this.setData({
            code : e.detail.value
        })
    },
    cancel(){
        this.setData({
            cvisible : false
        })
    },
    codesuccess(e){
        this.setData({
            'userinfo.level' : 1,
            'userinfo.endtime' : e.detail.time,
        })
    },
    showBenefit(){
        var self = this;
        self.setData({
            showCardBenefit : !self.data.showCardBenefit
        })
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    onReachBottom : function(){
        var self = this;

        if( self.data.isbot ) return false;
        self.data.isbot = true;

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list),
            });
        },function(){
            self.setData({
                isbot : false,
            });
        });
    },
    bindCode(){
        this.selectComponent("#ucardcode").showfn();
    },
    Nonmembers(e){
        var self = this;
        var index = e.currentTarget.dataset.index;
        if( index == self.data.membersnum ) return false;

        this.setData({
            isbot : false,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.doing' : false,
            'page.doo' : index == 0 ? 'ucard' : ( index == 1 ? 'ucard' : 'card' ),
            'page.pdata.op' : index == 0 ? 'ucardprize' : ( index == 1 ? 'ulist' : 'alllist' ),
        })
        
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
                membersnum : index,
            });
        })
    },
    changepak(e){
        var self = this;
        var postdata = {
            op : 'getpak',
            pakstyle : this.data.pakpage.pakstyle,
            paktype : this.data.pakpage.paktype,
            pakby : this.data.pakpage.pakby,
            lat : this.data.lat,
            lng : this.data.lng,
        };
        var type = e.currentTarget.dataset.type*1;
        if(type == 1){
            postdata.paktype = e.detail;
            this.data.pakpage.paktype = e.detail;
        }else if(type == 2){
            postdata.pakby = e.detail;
            this.data.pakpage.pakby = e.detail;
        }else if(type == 3){
            this.setData({
                'pakpage.pakstyle' : e.detail
            })
            return false;
        }
        app.com.http('ucard','POST',postdata,0,true,'','',function(res){
            if (res.data && !res.data.errno) {
                
                self.setData({
                    prizelist : res.data.data.list,
                })
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    changeulist(e){
        var self = this;
        var type = e.currentTarget.dataset.type*1;
        if(type == 1){
            this.data.page.pdata.ulevel = e.detail;
        }else if(type == 2){
            this.data.page.pdata.sortid = e.detail;
        }else if(type == 3){
            this.setData({
                'page.pdata.ustyle' : e.detail
            })
            return false;
        }
        this.setData({
            isbot : false,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.doing' : false,
        })
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list
            });
        })
    },
    shareit(){
        this.selectComponent('#poster').onClose();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },

})